package com.hupu.jrs.components.retrofit

import com.alibaba.fastjson.JSON
import okhttp3.ResponseBody
import retrofit2.Converter
import java.io.IOException
import java.lang.reflect.Type
import java.nio.charset.Charset

/**
 * Created by sll on 2016/3/31.
 */
class FastJsonResponseBodyConverter<T> : Converter<ResponseBody, T> {

    private var type: Type? = null
    private var charset: Charset? = null

    constructor() {}

    constructor(type: Type, charset: Charset) {
        this.type = type
        this.charset = charset
    }

    @Throws(IOException::class)
    override fun convert(value: ResponseBody): T {
        try {
            return JSON.parseObject(value.string(), type)
        } finally {
            value.close()
        }
    }
}
